package ar.edu.unicen.pladema.ui.model;

import org.eclipse.swt.graphics.RGB;

public class ColorProgress {
	private RGB from;
	
	private Double rRange;
	private Double gRange;
	private Double bRange;
	
	private int colorCount;
	
	
	public ColorProgress(RGB fromColor, RGB toColor) {
		this.from = fromColor;
		Double rCurrent = new Double(fromColor.red);// .getRed();
		Double gCurrent = new Double(fromColor.green);//getGreen();
		Double bCurrent = new Double(fromColor.blue);//getBlue();
		
		rRange = (rCurrent - toColor.red);
		gRange = (gCurrent - toColor.green);
		bRange = (bCurrent - toColor.blue);

		Double maxRango = absMax(rRange, gRange, bRange);
		
		colorCount = maxRango.intValue();
	}
	public String rangoToString() {
		return "[" + rRange.toString() + "," 
			+ gRange.toString() + ","
			+ bRange.toString() + "]";
	}
	
	private Double absMax(Double a, Double b, Double c) {
		return Math.max(Math.max(Math.abs(a), Math.abs(b)), Math.abs(c));
	}


	public int getColorCount() {
		return colorCount;
	}
	public RGB getColorAt(int maxR, int minR, int i) {
		if (i < minR) {
			System.out.println("i < minimo");
		}
		if (i > maxR) {
			System.out.println("i > maximo");
		}
		Double c = new Double(i - minR) / (maxR - minR);
		Double redMod = rRange*c;
		Double greenMod = gRange*c;
		Double blueMod = bRange*c;
		
		int rNew = from.red - redMod.intValue();
		int gNew = from.green - greenMod.intValue();
		int bNew = from.blue - blueMod.intValue();
		rNew =check(rNew);
		gNew =check(gNew);
		bNew =check(bNew);
		
		RGB result = new RGB(rNew , 
				gNew, 
				bNew);
		return result;
	}
	
	
	private int check(int bNew) {
		if (bNew < 0) return 0;
		if (bNew > 255) return 255;
		return bNew;
	}
	public static void main(final String [] args) {
		RGB red = new RGB(255, 0, 0);
		RGB green = new RGB(0, 128, 0);
		
		System.out.println("desde " + red.toString() + " a " + green.toString());
		
		ColorProgress cp = new ColorProgress(red, green);
		System.out.println("#colores: " + cp.getColorCount());
		System.out.println("#rango: " + cp.rangoToString());
		System.out.println("\t0> " + cp.getColorAt(255,0,0));
		System.out.println("\t255> " + cp.getColorAt(255,0,255));
		System.out.println("\t128> " + cp.getColorAt(255,0,128));
		System.out.println("\t1> " + cp.getColorAt(255,0,1));
		System.out.println("\t2> " + cp.getColorAt(255,0,2));
		System.out.println("\t3> " + cp.getColorAt(255,0,3));
		System.out.println("\t4> " + cp.getColorAt(255,0,4));
		System.out.println("\t4> " + cp.getColorAt(511,255,259));
//		System.out.println("\t> " + cp.next());
//		System.out.println("\t> " + cp.next());
//		System.out.println("\t> " + cp.next());
//		System.out.println("\t> " + cp.next());
//		System.out.println("\t> " + cp.next());
//		System.out.println("\t> " + cp.next());
//		System.out.println("\t> " + cp.next());
//		System.out.println("\t> " + cp.next());
//		System.out.println("\t> " + cp.next());
	}
}
